<?php

/************************************

EasyCB, Community Forum Software
Copyright (C) 2007  Jonathon D. Keogh <jonathon.keogh@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

$Id: page.topic.php 126 2007-09-05 23:43:04Z jonathon.keogh $

************************************/

require "inc.func.php";

$id = isset($_GET['id']) ? $_GET['id'] : 0;

$firstPost = db_query("SELECT * FROM `" . config_get("db_prefix") . "posts` WHERE `TopicID`='" . db_escape($id) . "';");
if(db_num_rows($firstPost) < 1)
{
	page_redirect("page.boards.php");
}
$firstPost = db_fetch_assoc($firstPost);

$topic = db_query("SELECT * FROM `" . config_get("db_prefix") . "topics` WHERE `ID`='" . db_escape($id) . "';");
if(db_num_rows($topic) < 1)
{
	page_redirect("page.boards.php");
}
$topic = db_fetch_assoc($topic);

page_header(htmlentities($firstPost['Title']));

// We need to work out the board trail
page_section();
$boards = Array();
$c = $topic['Board'];
while(1)
{
	$b = board_parent($c);
	$boards[$b] = $c;
	if($b == 0)
	{
		break;
	}
	$c = $b;
}
print "<a href=\"page.boards.php\">Home</a>";
while(1)
{
	if($boards[$c] == 0)
	{
		break;
	}
	print ": <a href=\"page.board.php?id=" . urlencode($boards[$c]) . "\">" . htmlentities(board_label($boards[$c])) . "</a>";
	if($boards[$c] == $topic['Board'])
	{
		break;
	}
	$c = $boards[$c];
}
print ": <a href=\"page.topic.php?id=" . urlencode($id) . "\">" . htmlentities($firstPost['Title']) . "</a>";
page_section();

page_section();
print "<a href=\"page.new.php?type=response&amp;id=" . urlencode($id) . "\">Respond to this topic</a>";
page_section();

?>

<script language="javascript">
<!--

/***

The mouse position part of this JavaScript is adapted from:
http://javascript.internet.com/page-details/mouse-coordinates.html

***/
var IE = document.all ? true : false;
if(!IE)
{
	document.captureEvents(Event.MOUSEMOVE)
}
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;
function getMouseXY(e)
{
	if(IE)
	{
		// grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {
		// grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	if(tempX < 0)
	{
		tempX = 0;
	}
	if(tempY < 0)
	{
		tempY = 0;
	}
	return true;
}

levels = new Array();
levels[0] = "";
levels[1] = "moderator";
levels[2] = "administrator";
function quickinfo_over(userID)
{
	i = document.getElementById("userinfo");
	
	i.innerHTML = "<b style=\"font-size: 14pt;\">" + quickInfo[userID]['Username'] + "</b><br>";
	if(quickInfo[userID]['Title'] != "")
	{
		i.innerHTML += "<i>" + quickInfo[userID]['Title'] + "</i><br>";
	}
	if(quickInfo[userID]['Name'] != "")
	{
		i.innerHTML += "Real name: " + quickInfo[userID]['Name'] + "<br>";
	}
	i.innerHTML += "Member since: " + quickInfo[userID]['MemberSince'] + "<br>";
	if(levels[quickInfo[userID]['Level']] != "")
	{
		i.innerHTML += "This user is a" + (levels[quickInfo[userID]['Level']][0] == "a" ? "n" : "") + " " + levels[quickInfo[userID]['Level']] + "<br>";
	}
	
	i.innerHTML += "<a style=\"color: #0000FF;\" href=\"page.user.php?id=" + userID + "\">More...</a>";
	
	i.style.visibility = "visible";
	i.style.left = tempX + 10;
	i.style.top = tempY + 10;
}
function quickinfo_out()
{
	if(tempOver != 0)
	{
		return;
	}
	i = document.getElementById("userinfo");
	i.innerHTML = "";
	i.style.visibility = "hidden";
}

quickInfo = new Array();
quickInfo[0] = new Array();
quickInfo[0]['Username'] = "<?=addslashes(htmlentities(config_get("UnknownUser"))) ?>";
quickInfo[0]['Title'] = "This user no longer exists";
quickInfo[0]['Name'] = "Not known";
quickInfo[0]['Level'] = 0;
quickInfo[0]['MemberSince'] = "Not known";

//-->
</script>

<div style="visibility: hidden; border-style: solid; border-color: #000000; border-width: 1px; padding: 5px; background-color: #FFFFFF; color: #000000; position: absolute;" id="userinfo"></div>

<?php

$unread_from = db_query("SELECT `NewPostFrom` FROM `" . config_get("db_prefix") . "unread` WHERE `UserID`='" . sess_get("UserID") . "' AND `TopicID`='" . db_escape($id) . "';");
if(db_num_rows($unread_from) > 0)
{
	$unread_from = db_fetch_assoc($unread_from);
	$unread_from = $unread_from['NewPostFrom'];
} else {
	$unread_from = 0;
}

$posts = db_query("SELECT * FROM `" . config_get("db_prefix") . "posts` WHERE `TopicID`='" . db_escape($id) . "';");
$pastunread = false;
while($post = db_fetch_assoc($posts))
{
	// Is this post unread onwards?
	if($unread_from == $post['ID'])
	{
		db_query("DELETE FROM `" . config_get("db_prefix") . "unread` WHERE `UserID`='" . sess_get("UserID") . "' AND `TopicID`='" . db_escape($id) . "';");
		print "<a name=\"unread\"></a>";
		$pastunread = true;
	}
	page_section(($post['ID'] == $firstPost['ID'] ? htmlentities($post['Title']) : ""));
	print "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n";
	print "<tr>\n";
	print "<td><a href=\"javascript:;\" onmouseover=\"tempOver={$post['UserID']};quickinfo_over({$post['UserID']});\" onmouseout=\"tempOver=0;setTimeout('quickinfo_out();', 4000);\">" . htmlentities(user_name($post['UserID'])) . "</a> said: (" . date(config_get("DateString"), $post['Time']) . ")" . ($pastunread ? " <b>[unread response]</b>" : "") . "</td>\n";
	print "</tr>\n";
	print "<tr>\n";
	print "<td style=\"padding-top: 5px; padding-bottom: 5px;\">" . nl2br(htmlentities($post['Content'])) . "</td>\n";
	print "</tr>\n";
	print "</table>\n";
	page_section();
	
	$user = db_query("SELECT * FROM `" . config_get("db_prefix") . "users` WHERE `ID`='" . db_escape($post['UserID']) . "';");
	if(db_num_rows($user) < 1)
	{
?>
<script language="javascript">
<!--

quickInfo[<?=$post['UserID'] ?>] = quickInfo[0];

//-->
</script>
<?php
	} else {
		$user = db_fetch_assoc($user);
?>

<script language="javascript">
<!--

quickInfo[<?=$post['UserID'] ?>] = new Array();
quickInfo[<?=$post['UserID'] ?>]['Username'] = "<?=addslashes(htmlentities(user_name($post['UserID']))) ?>";
quickInfo[<?=$post['UserID'] ?>]['Title'] = "<?=addslashes(htmlentities($user['Title'])) ?>";
quickInfo[<?=$post['UserID'] ?>]['Name'] = "<?=addslashes(htmlentities($user['Name'])) ?>";
quickInfo[<?=$post['UserID'] ?>]['Level'] = <?=$user['Level'] ?>;
quickInfo[<?=$post['UserID'] ?>]['MemberSince'] = "<?=date(config_get("DateString"), $user['RegisterTime']) ?>";

//-->
</script>

<?php
	}
}

// Increase the view count
db_query("UPDATE `" . config_get("db_prefix") . "topics` SET `ViewCount`='" . ($topic['ViewCount'] + 1) . "' WHERE `ID`='" . db_escape($id) . "';");

page_footer('$Id: page.topic.php 126 2007-09-05 23:43:04Z jonathon.keogh $');

?>